//
//  HWMineCollectionProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/22.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWMineCollectionProtocol: NSObject {
    var model:HWMineModel?
    /// 功能
    var navClick:((_ model:HWMineNavDataModel)->())?
    var headButtonClick:(()->())?
    var clickItem:((_:NSInteger,_:HWMineUserModel)->())?
    var signButtonClick:(()->())?
    var cardButtonClick:(()->())?
    var scoreButtonClick:(()->())?
    var coinButtonClick:(()->())?
    var appointmentButtonClick:(()->())?
}

extension HWMineCollectionProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section > 0 {
            self.navClick?((model?.nav[indexPath.section-1].data[indexPath.row])!)
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if section == 0 {
            return 5*widthScale
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        if section != 0 {
            return CGSize(width: screenWidth, height: 45*widthScale)
        }
        
        return CGSize.zero
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWMineHeaderView", for: indexPath) as! HWMineHeaderView
        header.titleLabel.text = model?.nav[indexPath.section-1].title
        return header
        
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: screenWidth, height: 180*widthScale)//340
        } else {
            return CGSize(width: screenWidth/4, height: 92*widthScale)
        }

    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 1 {
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        return UIEdgeInsets.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else {
            return model?.nav[section-1].data.count ?? 0
        }
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1 + (model?.nav.count ?? 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let mineCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineInfoCell", for: indexPath) as! HWMineInfoCell
        let itemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineItemCell", for: indexPath) as! HWMineItemCell
        if indexPath.section == 0 {
            mineCell.model = model?.user
            mineCell.infoModel = model
            mineCell.headButtonClick = {
                self.headButtonClick?()
            }
            mineCell.clickItem = {
                self.clickItem?($0,$1)
            }
            mineCell.signButtonClick = {
                self.signButtonClick?()
            }
            
            mineCell.cardButtonClick = {
                self.cardButtonClick?()
            }
            
            mineCell.scoreButtonClick = {
                self.scoreButtonClick?()
            }
            
            mineCell.coinButtonClick = {
                self.coinButtonClick?()
            }
            
            mineCell.appointmentButtonClick = {
                self.appointmentButtonClick?()
            }
            return mineCell
        } else {
            itemCell.model = model?.nav[indexPath.section-1].data[indexPath.row]
            return itemCell
        }
    }
    
}
